package com.gyh.thymeleaf.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyh.thymeleaf.entity.Building;
import com.gyh.thymeleaf.service.BuildingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gyhstart
 * @create 2020/6/26 - 14:02
 **/

@Api(tags = "房屋管理模块")
@Controller
@RequestMapping("/api")
public class BuildingController {
    private static final Logger logger = LoggerFactory.getLogger(BuildingController.class);

    @Autowired
    private BuildingService buildingService;

    /**
     * 跳转到楼栋管理界面
     * 查询数据
     */
    @GetMapping("/building")
    @ApiOperation(value = "楼栋界面", notes = "不需要参数", httpMethod = "GET")
    public String queryBuilding(Model model, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {
        //pageNum:表示第几页  pageSize:表示一页展示的数据
        PageHelper.startPage(pageNum, 5);
        List<Building> buildingsList = buildingService.query();
        //将查询到的数据封装到PageInfo对象
        PageInfo<Building> pageInfo = new PageInfo<>(buildingsList, 2);
        System.out.println("---------------------------------");
        System.out.println(pageInfo.getList());
        System.out.println("---------------------------------");
        model.addAttribute("buildingsList", buildingsList);
        model.addAttribute("pageInfo", pageInfo);
        return "/building/building";
    }

    /**
     * 查询数据ajax
     */
    @ResponseBody
    @GetMapping("/queryBuilding")
    @ApiOperation(value = "楼栋界面", notes = "不需要参数", httpMethod = "GET")
    public List<Building> queryBuildings(Model model) {
        List<Building> buildingsList = buildingService.query();
        return buildingsList;
    }

    /**
     * 分页查询数据ajax
     */
    @ResponseBody
    @GetMapping("/paging/{pageNum}")
    @ApiOperation(value = "分页楼栋界面", notes = "不需要参数", httpMethod = "GET")
    public PageInfo<Building> queryBuildingspaging(Model model, @PathVariable("pageNum") int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Building> buildingsList = buildingService.query();
        PageInfo<Building> pageInfo = new PageInfo<>(buildingsList);
        model.addAttribute("pageInfo", pageInfo);
        return pageInfo;
    }

    /**
     * 添加数据
     */
    @ResponseBody
    @PostMapping("/building")
    @ApiOperation(value = "楼栋添加界面", notes = "需要楼栋名称and类型", httpMethod = "POST")
    public int saveBuilding(@RequestBody String info) {
        Building building = JSON.parseObject(info, Building.class);
        return buildingService.save(building);
    }

    /**
     * 查询数据（回显）
     */
    @ResponseBody
    @GetMapping("/buildingById/{id}")
    @ApiOperation(value = "楼栋id查询界面", notes = "需要楼id", httpMethod = "GET")
    public Building queryBuildingsById(@PathVariable("id") int id) {
        return buildingService.queryById(id);
    }

    /**
     * 修改数据
     */
    @ResponseBody
    @PutMapping("/buildingById/{id}")
    @ApiOperation(value = "楼栋修改界面", notes = "需要楼栋名称and类型", httpMethod = "PUT")
    public int updateBuilding(@PathVariable("id") int id, @RequestBody String info) {
        System.out.println(info);
        Building building = JSON.parseObject(info, Building.class);
        return buildingService.modify(id, building);
    }

    /**
     * 删除数据
     */
    @ResponseBody
    @DeleteMapping("/buildingById/{id}")
    @ApiOperation(value = "楼栋删除界面", notes = "需要楼id", httpMethod = "DELETE")
    public int removeBuilding(@PathVariable("id") int id) {
        return buildingService.removeById(id);
    }

    /**
     * 条件查询数据
     */
    @ResponseBody
    @GetMapping("/building/{name}")
    @ApiOperation(value = "楼栋条件查询界面", notes = "需要楼名称", httpMethod = "GET")
    public PageInfo<Building> conditionBuilding(@PathVariable("name") String name,@RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Building> buildingsList = buildingService.conditionBuilding(name);
        PageInfo<Building> pageInfo = new PageInfo<>(buildingsList);
        return pageInfo;
    }
}
